package com.awesome.spring.saga.impl;

import com.awesome.spring.feign.AccountFeign;
import com.awesome.spring.saga.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * AccountServiceImpl
 *
 * @author alex wu
 * @date 2023/10/23
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountFeign accountFeign;

    @Override
    public boolean deductAccount(String userId, int money, boolean rollback) {
        System.out.println("userId: " + userId + ": money" + money);
        try {
            return accountFeign.deductAccount(userId, money, rollback);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public boolean compensateDeductAccount(String userId, int money) {
        System.out.println("userId: " + userId + ": money" + money);
        try {
            return accountFeign.compensateDeductAccount(userId, money);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
